﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommDll;

namespace InstitutionAssitant
{
    /// <summary>
    /// ClassName:frmDbConfig
    /// Version:1.0
    /// Date:2016-06-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现数据库连接设置
    /// </remarks>
    public partial class frmDbConfig : Form
    {
        #region 0.自定义属性
        public DbHelper_Abstract m_DbHelper = null;// 数据库连接说明
        public string m_strDbKindKeyWord = "";
        public string m_strConnectionStringKeyWord = "";
        #endregion

        public frmDbConfig()
        {
            InitializeComponent();
        }

        #region  1.自定义方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            // 00 初始化标题
            this.Text = string.Format("数据库连接设置({0})", m_DbHelper.m_ConnectInfo.strFlag, m_DbHelper.m_ConnectInfo.strDbKind);
            lblTitle.Text = this.Text;
            // 01 显示数据连接项值
            m_strDbKindKeyWord = m_DbHelper.m_ConnectInfo.strDbKindKeyWord;
            m_strConnectionStringKeyWord = m_DbHelper.m_ConnectInfo.strConnectionStringKeyWord;
            m_DbHelper._getConnectInfo();
            txtServerName.Text = m_DbHelper.m_ConnectInfo.strServerName;
            txtPort.Text = m_DbHelper.m_ConnectInfo.strPort;
            txtDbName.Text = m_DbHelper.m_ConnectInfo.strDbName;
            txtDbUser.Text = m_DbHelper.m_ConnectInfo.strUser;
            txtDbPassword.Text = m_DbHelper.m_ConnectInfo.strPassword;
            txtHisInterface.Text = DbHelper._getHisAddress();
            txtVersion.Text = DbHelper._getAutoUpdateAddress();
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        private void _saveInfo()
        {
            string strDbKind = "";
            strDbKind = rbSQL.Checked ? "SQL" : strDbKind;
            strDbKind = rbOracle.Checked ? "ORACLE" : strDbKind;
            strDbKind = rbMySql.Checked ? "MYSQL" : strDbKind;
            if (!strDbKind.Equals(m_DbHelper.m_ConnectInfo.strDbKind))
            {
                switch (strDbKind)
                {
                    case "SQL":
                        m_DbHelper = new DbHelper_Sql();
                        break;
                    case "ORACLE":
                        m_DbHelper = new DbHelper_Oracle();
                        break;
                    case "MYSQL":
                        m_DbHelper = new DbHelper_MySql();
                        break;
                    default:
                        m_DbHelper = new DbHelper_Sql();
                        break;
                }
            }
            string strTemp = "";
            DbHelper._setHisAddress(txtHisInterface.Text.Trim());
            DbHelper._setAutoUpdateAddress(txtVersion.Text.Trim());
            //FunctionComm._setAppConfigKeyValue("AutoUpdateAddress", txtVersion.Text.Trim());
            //FunctionComm._setAppConfigKeyValue("HisInterFaceAddress", txtHisInterface.Text.Trim());
            // 01 保存数据库连接信息            
            m_DbHelper.m_ConnectInfo.strServerName = txtServerName.Text.Trim();
            m_DbHelper.m_ConnectInfo.strPort = txtPort.Text.Trim();
            m_DbHelper.m_ConnectInfo.strDbName = txtDbName.Text.Trim();
            m_DbHelper.m_ConnectInfo.strUser = txtDbUser.Text.Trim();
            m_DbHelper.m_ConnectInfo.strPassword = txtDbPassword.Text.Trim();
            m_DbHelper.m_ConnectInfo.strConnectionStringKeyWord = m_strConnectionStringKeyWord;

            m_DbHelper.m_ConnectInfo.strDbKind = strDbKind;
            m_DbHelper.m_ConnectInfo.strDbKindKeyWord = m_strDbKindKeyWord;
            // 02 生成连接信息
            m_DbHelper._makeConnectInfo();
            // 03 数据库连接成功则保存到AppConfig
            m_DbHelper.m_bConnected = m_DbHelper._connectDB();
            if (m_DbHelper.m_bConnected)
            {
                // 更新数据库连接字符串信息
                FunctionComm._setAppConfigKeyValue(string.Format("{0}", m_DbHelper.m_ConnectInfo.strDbKindKeyWord), strDbKind);
                strTemp = FunctionComm._encryptDES(m_DbHelper.m_ConnectInfo.strConnectionString, "taihao12");
                FunctionComm._setAppConfigKeyValue(string.Format("{0}", m_DbHelper.m_ConnectInfo.strConnectionStringKeyWord), strTemp);

                MessageBox.Show("数据库连接成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("数据库连接失败,请重新设置！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

        #region 2.系统事件方法
        private void frmDbConfig_Load(object sender, EventArgs e)
        {
            _init();
        }

        private void frmDbConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            Boolean bResult = m_DbHelper.m_bConnected;
            if (!bResult && !m_DbHelper.m_ConnectInfo.strDbKind.Equals("OLEDB"))  
            {
                //数据库联接失败提示
                DialogResult result = MessageBox.Show("是否确定退出数据库设置，系统将不能继续运行?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _saveInfo();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}